package exercise;

import java.util.ArrayList;
import java.util.function.IntConsumer;

public class ZeroEvenOdd {
	int n;
	public volatile int flag =1;
	public ZeroEvenOdd(int n) {
		super();
		this.n = n;
	}
	

	public void zero(IntConsumer printer) throws Exception{
			for(int i=1;i<=n;i++){
				synchronized (this) {
					while(flag%2==0){
						this.wait();
					}
					flag++;
//					this.printNumber(0);
					printer.accept(0);
					this.notifyAll();
				}
			}
	
	}
	
	public void even(IntConsumer printer) throws Exception{
			for(int i=2;i<=n;i+=2){
				synchronized (this) {
					while(flag%4!=0){
						this.wait();
					}
					flag++;
//					this.printNumber(i);
					printer.accept(i);
					this.notifyAll();
				}
			}	
	}
	public void odd(IntConsumer printer) throws Exception{
			for(int i=1;i<=n;i+=2){
				synchronized (this) {
					while(flag%4!=2){
						this.wait();
					}
//					this.printNumber(i);
					printer.accept(i);
					flag++;
					this.notifyAll();
				}
			}	
			/*synchronized (this) {
				while(true){
					if(count==list.size()){
						break;
					}
					while(list.get(count)%2==0){
						try {
							this.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					this.printNumber(list.get(count));
					count++;
					this.notifyAll();
				}
			}	*/
	}
	public static void printNumber(int n){
		System.out.print(n);
	}
}
